import { createRouter, createWebHistory } from "vue-router";
import HomeView from "../views/HomeView.vue";
import { useCounterStore } from "@/stores/counter";

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: "/",
      name: "Login",
      component: () => import("../views/Login.vue"),
    },
    {
      path: "/home",
      name: "home",
      component: HomeView,
    },
    {
      path: "/about",
      name: "about",
      component: () => import("../views/AboutView.vue"),
    },
    {
      path: "/Main",
      name: "Main",
      component: () => import("../views/Main.vue"),
      children: [
        {
          path: "/UserList",
          name: "UserList",
          component: () => import("../views/UserList.vue"),
        },
        {
          path: "/RoleList",
          name: "RoleList",
          component: () => import("../views/RoleList.vue"),
        },
        {
          path: "/PermissionList",
          name: "PermissionList",
          component: () => import("../views/PermissionList.vue"),
        },
  
      ],
    },
  ],
});

//配置导航守卫
router.beforeEach((to, from, next) => {
  if (to.path != "/" && localStorage.getItem("IsLogin") === "") {
    //非登录页面且未登录无token
    next("/"); //无法去往其他页面
  } else {
    next(); //登录后可前往其他页面
  }
});

export default router;
